﻿Imports System.Data.OleDb

Public Class EditSelectedVideo
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack = False Then   'we have come here from another page
            If Request.Params("VideoID") = "" Then Response.Redirect("Artists.aspx") 'if no params in ID, then back to start
            'create a data reader field
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("3dBuzzDatabase").ConnectionString)
            Dim SqlString As String = "SELECT Video.[VideoId], Video.[Title], Video.[Description], Video.[OriginalFilename] FROM Video WHERE [VideoId] = @f1"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", Request.Params("VideoId"))
            oleDbConn.Open()
            'this is the data reader
            Dim dataReader = cmd.ExecuteReader()    'because we are returning results
            dataReader.Read()
            If dataReader.Item("Title") = "" Then
                tb_Title.Text = ""
            Else
                tb_Title.Text = dataReader.Item("Title")
            End If

            '  If IsDBNull(dataReader.Item("OriginalFilename")) Then   'do you know how long this took me to sort out?  30mins - is that good or bad?!
            'tb_OriginalFilename.Text = ""
            'Else

            'tb_OriginalFilename.Text = dataReader.Item("OriginalFilename")
            'End If

            If dataReader.Item("Description") = "" Then
                tb_Description.Text = ""
            Else

                tb_Description.Text = dataReader.Item("Description")
            End If


        End If

    End Sub

    Protected Sub btn_Submit_Click(sender As Object, e As EventArgs) Handles btn_Submit.Click
        Dim oleDbConn2 As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("3dBuzzDatabase").ConnectionString)
        Dim sqlUpdateString As String = "UPDATE Video SET Video.[Title]= @f3, Video.[Description]=@f4 WHERE Video.[VideoId] = @f2"

        Dim updatecmd As OleDbCommand = New OleDbCommand(sqlUpdateString, oleDbConn2)
        updatecmd.CommandType = CommandType.Text

        updatecmd.Parameters.AddWithValue("@f3", tb_Title.Text)
        updatecmd.Parameters.AddWithValue("@f4", tb_Description.Text)
        

        updatecmd.Parameters.AddWithValue("@f2", Request.Params("VideoId"))

        oleDbConn2.Open()
        updatecmd.ExecuteNonQuery()
        oleDbConn2.Close()
        oleDbConn2.Dispose()
        Response.Redirect("~/EditShowreel.aspx")
    End Sub
End Class